*** Prep for discontinutiy
gen ltv_diff = oltv - 105
gen hi_ltv = oltv > 105

gen homevalue = orig_amount/ oltv* 100
drop if homevalue == .

gen ltvprev = min(prin_bal_amount*1.04, prin_bal_amount + 5000)/ homevalue* 100

gen ltvprev_bin =  ceil(ltvprev)
gen hi_ltvprev = ltvprev >= 105.01
gen ltvprev_diff = ltvprev- 105.01

gen tbaelig = oltv <= 105
gen notba = 1-tbaelig

egen zip_ym_seller_id = group(seller_id_name zip_3 orig_ym)
egen seller_id_id = group(seller_id_name)
egen servicer_id = group(servicer_name)

*** convert string variables to categorical variables

egen prop_type_id_id = group(prop_type_id)
gen PropCO = prop_type_id == "CO"
gen PropPU = prop_type_id == "PU"

replace orig_chn = channel if orig_chn == ""
drop channel
egen orig_chan_id = group(orig_chn)
gen BrokerOrig = orig_chn == "B"
gen CorrespondentOrig = orig_chn == "C"

gen mi_ins = mi_pct >0 & mi_pct ~=.
replace mi_pct = 0 if mi_ins == 0

// LLPA bins
do "${code}/tba_elig/_LLPAbins" credit_scorecore_b oltv
egen LLPAbin = group(ltv_bin credit_score_bin)



